#!/usr/bin/python
# coding=utf8
#
# Copyright (C) 2011 XNData contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__author__ = "zhangchitc@gmail.com"


import cookielib
import urllib2, urllib
import xndata.client
from xndata.frontend.photo import PhotoPageFetcher
from xndata.frontend.photo import PhotoPageParser

class XNPhotoClient (xndata.client.XNClient):

    def __init__ (self, xnclient=None, uname=None, upass=None):
        if xnclient is not None:
            # initialize with already-logined xnclient
            self.opener = xnclient.opener
            self.user_id = xnclient.user_id
            self.user_name = xnclient.user_name

        if (uname is not None) and (upass is not None):
            # initialize with username and password
            xndata.client.XNClient.__init__ (self, uname, upass)


    def getTotalAlbum (self, user_id):
        html = PhotoPageFetcher.fetch_album_list_page (self.opener, user_id, 0)
        return PhotoPageParser.get_total_album (html)

    def getTotalAlbumList (self, user_id):
        return (self.getTotalAlbum (user_id) - 1) / 16 + 1

    def getNextAlbumList (self, user_id=None, prev_album_list=None):
        if user_id is not None:
            html = PhotoPageFetcher.fetch_album_list_page (self.opener, user_id, 0)
            next_album_list = PhotoPageParser.get_album_list (html, user_id, 0)
        else:
            html = PhotoPageFetcher.fetch_album_list_page (self.opener, 
                        prev_album_list.owner_id, prev_album_list.cur_page + 1)
            next_album_list = PhotoPageParser.get_album_list (html, 
                        prev_album_list.owner_id, prev_album_list.cur_page + 1)
        return next_album_list

    def getAllAlbum (self, user_id):
        total_album_list = self.getTotalAlbumList (user_id)
        all_album_list = self.getNextAlbumList (user_id=user_id)
        prev_res = all_album_list
        for i in range (1, total_album_list):
            tmp_album_list = self.getNextAlbumList (prev_album_list=prev_res)
            all_album_list.album_list.extend (tmp_album_list.album_list)
            prev_res = tmp_album_list

        return all_album_list

    def getNextPhotoList (self, user_id=None, album_id=None, prev_photo_list=None):
        if (user_id is not None) and (album_id is not None):
            html = PhotoPageFetcher.fetch_photo_list_page (self.opener, 
                            user_id, album_id, 0)
            next_photo_list = PhotoPageParser.get_photo_list (html, 
                            user_id, album_id, 0)
        else:
            html = PhotoPageFetcher.fetch_photo_list_page (self.opener,
                    prev_photo_list.owner_id, prev_photo_list.album_id,
                    prev_photo_list.cur_page + 1)
            next_photo_list = PhotoPageParser.get_photo_list (html,
                    prev_photo_list.owner_id, prev_photo_list.album_id,
                    prev_photo_list.cur_page + 1)
        return next_photo_list 

    def getTotalPhoto (self, user_id, album_id):
        html = PhotoPageFetcher.fetch_photo_list_page (self.opener, 
                            user_id, album_id, 0)
        return PhotoPageParser.get_total_photo (html)

    def getTotalPhotoList (self, user_id, album_id):
        return (self.getTotalPhoto (user_id, album_id) - 1) / 20 + 1

    def getAllPhoto (self, user_id, album_id):
        total_photo_list = self.getTotalPhotoList (user_id, album_id)
        all_photo_list = self.getNextPhotoList (user_id=user_id, album_id = album_id)
        prev_res = all_photo_list
        for i in range (1, total_photo_list):
            tmp_photo_list = self.getNextPhotoList (prev_photo_list=prev_res)
            all_photo_list.photo_list.extend (tmp_photo_list.photo_list)
            prev_res = tmp_photo_list

        return all_photo_list

    def getPhotoDetail (self, user_id=None, photo_id=None, photo_profile=None):
        if (user_id is not None) and (photo_id is not None):
            html = PhotoPageFetcher.fetch_photo_page (self.opener,
                            user_id, photo_id)
            photo_detail = PhotoPageParser.get_photo_detail (html, 
                            user_id, photo_id)
        else:
            html = PhotoPageFetcher.fetch_photo_page (self.opener,
                            photo_profile.owner_id, photo_profile.photo_id)
            photo_detail = PhotoPageParser.get_photo_detail (html,
                            photo_profile.owner_id, photo_profile.photo_id)
        
        return photo_detail



if __name__ == "__main__":
    client = XNPhotoClient (uname="zhangchitc@gmail.com", upass="aaaaaaaa")
    
    # Other's albums
    # album_list = client.getNextAlbumList (user_id="247780420")
    # print album_list


    # My albums
    # album_list = client.getNextAlbumList (user_id="242124580")
    # print album_list

    #album_list = client.getNextAlbumList (prev_album_list=album_list)
    # print album_list

    #album_list = client.getAllAlbumList (user_id="242124580")
    # print album_list


    # other's album
    #photo_list = client.getNextPhotoList (user_id="247780420", album_id="282652960")
    #print photo_list
    # my album
    #photo_list = client.getNextPhotoList (user_id="242124580", album_id="432244996")
    #print photo_list
    #print client.getAllPhoto ("242124580", "432244996")
    #print client.getAllPhoto ("247780420", "282652960")
    print client.getAllPhoto ("252475509", "265669703")
    #print client.getAllPhoto ("242124580", "272296924")
